﻿/********************************************************************************************
 * Time de desenvolvedores
 *  Vinícius Batista de Souza       @vbs_br
 *  Fermando Martin Figuera         @fernandomartin
 *  
 *  
 * Jogo desenvolvido para testar algumas features e produzir trilha de treinamento no MSDN.
 * Faça o download do SDK do Windows Phone, faça você também o seu jogo :)
 * 
 * Dúvidas utilizem o Forúm do Windows Phone no MSDN.
********************************************************************************************/



using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;


namespace ComboBUG
{
    public partial class UserControlElemento : UserControl, IComparable
    {

        static bool ic_clicado = false;
        private bool Verificado_Se_Igual;

        
        #region UserControlElemento
        public UserControlElemento()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(UserControlElemento_Loaded);
        }
        #endregion

        #region UserControlEemento_Loaded
        void UserControlElemento_Loaded(object sender, RoutedEventArgs e)
        {

            Verificado_Se_Igual = false;

        }
        #endregion

        #region SetTAG
        public void SetTAG(int Tag)
        {
            UC.Tag = Tag;
        }
        #endregion

        #region GetTAG
        public int GetTAG()
        {
            return (int)UC.Tag;
        }
        #endregion

        #region LiberaClick
        public void LiberaClick()
        {
            ic_clicado = false;
        }
        #endregion

        #region UC_MouseLeftButtonDown
        private void UC_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            // Verifica se o Item foi clicado , se sim
            // aguarda a liberação da tela.

            if (ic_clicado == true) { return; }
            if (ic_clicado == false)
            {
                ic_clicado = true;
            }


            GamePage pg = ControlFinder.FindParent<GamePage>(this);
            WrapPanel wp = (WrapPanel)pg.FindName("WrapPanelPrincipal");

            if (wp.Children.Contains(this))
            {
                pg.contador_recursividade = 0;
                
                pg.ProcuraElementosParaRemover(this);

                pg.VerificaElementos();

            }


        }
        #endregion

        #region EhIgual
        public bool EhIgual(string tag)
        {

            string tagUC = this.UC.Tag.ToString();

            if (tagUC == tag)
            {
                Verificado_Se_Igual = true;

                return GetVerificado();
            }

            return false;

        }
        #endregion

        #region GetVerificado
        public bool GetVerificado()
        {
            return Verificado_Se_Igual;
        }
        #endregion

        #region LimpaVerificacao
        public void LimpaVerificacao()
        {
            this.Verificado_Se_Igual = false;
        }
        #endregion

        #region Explode
        public void Explode()
        {
            this.Tag = "BOOM";
        }
        #endregion

        #region ExplosaoAnimacao
        public void ExplosaoAnimacao()
        {
            StoryboardExplosao.Begin();
        }
        #endregion

        #region RetornaImagem
        public string RetornaImagem(int numero)
        {
            String str_img = "";

            if (numero == 1)
            {
                str_img = "/ComboBUG;component/Media/Images/vermelha1.png";

            }
            else if (numero == 2)
            {
                str_img = "/ComboBUG;component/Media/Images/azul1.png";
            }
            else if (numero == 3)
            {
                str_img = "/ComboBUG;component/Media/Images/amarela1.png";

            }
            else if (numero == 4)
            {
                str_img = "/ComboBUG;component/Media/Images/verde1.png";
            }
            else if (numero == 5)
            {
                str_img = "/ComboBUG;component/Media/Images/roxa1.png";
            }
            else if (numero == 6)
            {
                str_img = "/ComboBUG;component/Media/Images/rosa1.png";
            }
            else if (numero == 7)
            {
                str_img = "/ComboBUG;component/Media/Images/colorida1.png";
            }
            else if (numero == 8)
            {
                str_img = "/ComboBUG;component/Media/Images/aranha1.png";

            }
            else if (numero == 22)
            {
                
                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/vermelha2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/vermelha1.png";
                }
            }
            else if (numero == 32)
            {
                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/azul2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/azul1.png";
                }
            }
            else if (numero == 42)
            {

                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/amarela2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/amarela1.png";
                }
                
            }
            else if (numero == 52)
            {
                Random r = new Random();
                int num_ = r.Next(1,2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/verde2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/verde1.png";
                }
            }
            else if (numero == 62)
            {
                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/roxa2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/roxa1.png";
                }
            }
            else if (numero == 72)
            {
                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/rosa2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/rosa1.png";
                }               
            }
            else if (numero == 82)
            {
                Random r = new Random();
                int num_ = r.Next(1, 2);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/colorida2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/colorida1.png";
                }
            }

            else if (numero == 92)
            {

                Random r = new Random();
                int num_ = r.Next(1, 3);

                if (num_ == 1)
                {
                    str_img = "/ComboBUG;component/Media/Images/aranha2.png";
                }
                else if (num_ == 2)
                {
                    str_img = "/ComboBUG;component/Media/Images/aranha3.png";
                }
                else if (num_ == 3)
                {
                    str_img = "/ComboBUG;component/Media/Images/aranha4.png";
                }

            }

            else if (numero == 999)
            {
                // Reiniciar 
                str_img = "/ComboBUG;component/Media/Images/refresh.png";
            }

            return str_img;



        }
        #endregion

        #region TrocaImagem
        public void TrocaImagem(int numero, double size=0)
        {

            String str_img = RetornaImagem(numero);

            //Image img = new Image();
            BitmapImage bmp = new BitmapImage(new Uri(str_img, UriKind.Relative));
            //img.Source = bmp;

            this.image1.Source = bmp;
            if (size != 0)
            {
                this.image1.Width = size;
                this.image1.Height = size;
            }
        }
        #endregion

        #region ControlFinder

        public static class ControlFinder
        {
            public static T FindParent<T>(UIElement control) where T : UIElement
            {
                UIElement p = VisualTreeHelper.GetParent(control) as UIElement;
                if (p != null)
                {
                    if (p is T)
                        return p as T;
                    else
                        return ControlFinder.FindParent<T>(p);
                }
                return null;
            }
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {

            if (obj is UserControlElemento)
                return this.UC.Tag.ToString().CompareTo(((UserControlElemento)obj).UC.Tag.ToString());
            else
                return 0;
        }

        #endregion
    }
}
